<!DOCTYPE HTML>
<html lang="en" >
    <!-- Start book 6节课入门Flask框架web开发 -->
    <head>
        <!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>Jinja2模板引擎 | 6节课入门Flask框架web开发</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../chapter2/guo-lv-qi.html" />
    
    
    <link rel="prev" href="../chapter2.html" />
    

        <!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
        
    <div class="book"
        data-level="2.1"
        data-chapter-title="Jinja2模板引擎"
        data-filepath="chapter2/jinja2mo-ban-yin-qing.md"
        data-basepath=".."
        data-revision="Sun Jan 28 2018 01:59:46 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        Flask快速入门课程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="chapter1.html">
            
                
                    <a href="../chapter1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        Flask基础
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="chapter1/flaskjian-jie.html">
            
                
                    <a href="../chapter1/flaskjian-jie.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        为什么要使用WEB框架
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="chapter1/flaskyu-django-dui-bi.html">
            
                
                    <a href="../chapter1/flaskyu-django-dui-bi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        Flask简介
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="chapter1/flask-an-zhuang-huan-jing.html">
            
                
                    <a href="../chapter1/flask-an-zhuang-huan-jing.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        Flask 安装环境
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="chapter1/requirements-wen-jian.html">
            
                
                    <a href="../chapter1/requirements-wen-jian.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        requirements 文件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="chapter1/hello-worldcheng-xu.html">
            
                
                    <a href="../chapter1/hello-worldcheng-xu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        Hello World程序
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="chapter1/lu-you-de-ge-zhong-ding-yi-fang-shi.html">
            
                
                    <a href="../chapter1/lu-you-de-ge-zhong-ding-yi-fang-shi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        路由定义的基本方式
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="chapter2.html">
            
                
                    <a href="../chapter2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        模板
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="2.1" data-path="chapter2/jinja2mo-ban-yin-qing.html">
            
                
                    <a href="../chapter2/jinja2mo-ban-yin-qing.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        Jinja2模板引擎
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="chapter2/guo-lv-qi.html">
            
                
                    <a href="../chapter2/guo-lv-qi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        过滤器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="chapter2/hong.html">
            
                
                    <a href="../chapter2/hong.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        Flask-WTF表单
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="chapter3.html">
            
                
                    <a href="../chapter3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        数据库
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="chapter3/shu-ju-ku-de-she-zhi.html">
            
                
                    <a href="../chapter3/shu-ju-ku-de-she-zhi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        Flask中使用数据库
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="chapter3/shu-ju-ku-ji-ben-cao-zuo.html">
            
                
                    <a href="../chapter3/shu-ju-ku-ji-ben-cao-zuo.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        数据库基本操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="chapter3/zong-he-an-4f8b-tu-shu-guan-li.html">
            
                
                    <a href="../chapter3/zong-he-an-4f8b-tu-shu-guan-li.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        综合案例-图书管理
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.3.1" data-path="chapter3/zong-he-an-4f8b-tu-shu-guan-li/ding-yi-mo-xing.html">
            
                
                    <a href="../chapter3/zong-he-an-4f8b-tu-shu-guan-li/ding-yi-mo-xing.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.1.</b>
                        
                        定义模型&amp;创建表&amp;数据添加
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3.2" data-path="chapter3/zong-he-an-4f8b-tu-shu-guan-li/shu-ju-xian-793a26-biao-dan-tian-jia.html">
            
                
                    <a href="../chapter3/zong-he-an-4f8b-tu-shu-guan-li/shu-ju-xian-793a26-biao-dan-tian-jia.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.2.</b>
                        
                        数据显示&amp;表单添加
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3.3" data-path="chapter3/zong-he-an-4f8b-tu-shu-guan-li/biao-dan-yan-zheng.html">
            
                
                    <a href="../chapter3/zong-he-an-4f8b-tu-shu-guan-li/biao-dan-yan-zheng.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.3.</b>
                        
                        添加数据的表单验证
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3.4" data-path="chapter3/zong-he-an-4f8b-tu-shu-guan-li/shan-chu-shu-ju.html">
            
                
                    <a href="../chapter3/zong-he-an-4f8b-tu-shu-guan-li/shan-chu-shu-ju.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.4.</b>
                        
                        删除数据
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >6节课入门Flask框架web开发</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="jinja2&#x6A21;&#x677F;&#x5F15;&#x64CE;">Jinja2&#x6A21;&#x677F;&#x5F15;&#x64CE;</h1>
<h2 id="&#x6A21;&#x677F;">&#x6A21;&#x677F;</h2>
<p>&#x5728;&#x524D;&#x9762;&#x7684;&#x793A;&#x4F8B;&#x4E2D;&#xFF0C;&#x89C6;&#x56FE;&#x51FD;&#x6570;&#x7684;&#x4E3B;&#x8981;&#x4F5C;&#x7528;&#x662F;&#x751F;&#x6210;&#x8BF7;&#x6C42;&#x7684;&#x54CD;&#x5E94;&#xFF0C;&#x8FD9;&#x662F;&#x6700;&#x7B80;&#x5355;&#x7684;&#x8BF7;&#x6C42;&#x3002;&#x5B9E;&#x9645;&#x4E0A;&#xFF0C;&#x89C6;&#x56FE;&#x51FD;&#x6570;&#x6709;&#x4E24;&#x4E2A;&#x4F5C;&#x7528;&#xFF1A;&#x5904;&#x7406;&#x4E1A;&#x52A1;&#x903B;&#x8F91;&#x548C;&#x8FD4;&#x56DE;&#x54CD;&#x5E94;&#x5185;&#x5BB9;&#x3002;&#x5728;&#x5927;&#x578B;&#x5E94;&#x7528;&#x4E2D;&#xFF0C;&#x628A;&#x4E1A;&#x52A1;&#x903B;&#x8F91;&#x548C;&#x8868;&#x73B0;&#x5185;&#x5BB9;&#x653E;&#x5728;&#x4E00;&#x8D77;&#xFF0C;&#x4F1A;&#x589E;&#x52A0;&#x4EE3;&#x7801;&#x7684;&#x590D;&#x6742;&#x5EA6;&#x548C;&#x7EF4;&#x62A4;&#x6210;&#x672C;&#x3002;&#x672C;&#x8282;&#x5B66;&#x5230;&#x7684;&#x6A21;&#x677F;&#xFF0C;&#x5B83;&#x7684;&#x4F5C;&#x7528;&#x5373;&#x662F;&#x627F;&#x62C5;&#x89C6;&#x56FE;&#x51FD;&#x6570;&#x7684;&#x53E6;&#x4E00;&#x4E2A;&#x4F5C;&#x7528;&#xFF0C;&#x5373;&#x8FD4;&#x56DE;&#x54CD;&#x5E94;&#x5185;&#x5BB9;&#x3002; </p>
<ul>
<li>&#x6A21;&#x677F;&#x5176;&#x5B9E;&#x662F;&#x4E00;&#x4E2A;&#x5305;&#x542B;&#x54CD;&#x5E94;&#x6587;&#x672C;&#x7684;&#x6587;&#x4EF6;&#xFF0C;&#x5176;&#x4E2D;&#x7528;&#x5360;&#x4F4D;&#x7B26;(&#x53D8;&#x91CF;)&#x8868;&#x793A;&#x52A8;&#x6001;&#x90E8;&#x5206;&#xFF0C;&#x544A;&#x8BC9;&#x6A21;&#x677F;&#x5F15;&#x64CE;&#x5176;&#x5177;&#x4F53;&#x7684;&#x503C;&#x9700;&#x8981;&#x4ECE;&#x4F7F;&#x7528;&#x7684;&#x6570;&#x636E;&#x4E2D;&#x83B7;&#x53D6;</li>
<li>&#x4F7F;&#x7528;&#x771F;&#x5B9E;&#x503C;&#x66FF;&#x6362;&#x53D8;&#x91CF;&#xFF0C;&#x518D;&#x8FD4;&#x56DE;&#x6700;&#x7EC8;&#x5F97;&#x5230;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x8FD9;&#x4E2A;&#x8FC7;&#x7A0B;&#x79F0;&#x4E3A;&#x201C;&#x6E32;&#x67D3;&#x201D;</li>
<li>Flask&#x662F;&#x4F7F;&#x7528; <strong>Jinja2</strong> &#x8FD9;&#x4E2A;&#x6A21;&#x677F;&#x5F15;&#x64CE;&#x6765;&#x6E32;&#x67D3;&#x6A21;&#x677F;</li>
</ul>
<p>&#x4F7F;&#x7528;&#x6A21;&#x677F;&#x7684;&#x597D;&#x5904;&#xFF1A;</p>
<ul>
<li>&#x89C6;&#x56FE;&#x51FD;&#x6570;&#x53EA;&#x8D1F;&#x8D23;&#x4E1A;&#x52A1;&#x903B;&#x8F91;&#x548C;&#x6570;&#x636E;&#x5904;&#x7406;(&#x4E1A;&#x52A1;&#x903B;&#x8F91;&#x65B9;&#x9762;)</li>
<li>&#x800C;&#x6A21;&#x677F;&#x5219;&#x53D6;&#x5230;&#x89C6;&#x56FE;&#x51FD;&#x6570;&#x7684;&#x6570;&#x636E;&#x7ED3;&#x679C;&#x8FDB;&#x884C;&#x5C55;&#x793A;(&#x89C6;&#x56FE;&#x5C55;&#x793A;&#x65B9;&#x9762;)</li>
<li>&#x4EE3;&#x7801;&#x7ED3;&#x6784;&#x6E05;&#x6670;&#xFF0C;&#x8026;&#x5408;&#x5EA6;&#x4F4E;</li>
</ul>
<h2 id="jinja2">Jinja2</h2>
<h3 id="&#x4E24;&#x4E2A;&#x6982;&#x5FF5;&#xFF1A;">&#x4E24;&#x4E2A;&#x6982;&#x5FF5;&#xFF1A;</h3>
<ul>
<li>Jinja2&#xFF1A;&#x662F; Python &#x4E0B;&#x4E00;&#x4E2A;&#x88AB;&#x5E7F;&#x6CDB;&#x5E94;&#x7528;&#x7684;&#x6A21;&#x677F;&#x5F15;&#x64CE;&#xFF0C;&#x662F;&#x7531;Python&#x5B9E;&#x73B0;&#x7684;&#x6A21;&#x677F;&#x8BED;&#x8A00;&#xFF0C;&#x4ED6;&#x7684;&#x8BBE;&#x8BA1;&#x601D;&#x60F3;&#x6765;&#x6E90;&#x4E8E; Django &#x7684;&#x6A21;&#x677F;&#x5F15;&#x64CE;&#xFF0C;&#x5E76;&#x6269;&#x5C55;&#x4E86;&#x5176;&#x8BED;&#x6CD5;&#x548C;&#x4E00;&#x7CFB;&#x5217;&#x5F3A;&#x5927;&#x7684;&#x529F;&#x80FD;&#xFF0C;&#x5176;&#x662F;Flask&#x5185;&#x7F6E;&#x7684;&#x6A21;&#x677F;&#x8BED;&#x8A00;&#x3002;</li>
<li>&#x6A21;&#x677F;&#x8BED;&#x8A00;&#xFF1A;&#x662F;&#x4E00;&#x79CD;&#x88AB;&#x8BBE;&#x8BA1;&#x6765;&#x81EA;&#x52A8;&#x751F;&#x6210;&#x6587;&#x6863;&#x7684;&#x7B80;&#x5355;&#x6587;&#x672C;&#x683C;&#x5F0F;&#xFF0C;&#x5728;&#x6A21;&#x677F;&#x8BED;&#x8A00;&#x4E2D;&#xFF0C;&#x4E00;&#x822C;&#x90FD;&#x4F1A;&#x628A;&#x4E00;&#x4E9B;&#x53D8;&#x91CF;&#x4F20;&#x7ED9;&#x6A21;&#x677F;&#xFF0C;&#x66FF;&#x6362;&#x6A21;&#x677F;&#x7684;&#x7279;&#x5B9A;&#x4F4D;&#x7F6E;&#x4E0A;&#x9884;&#x5148;&#x5B9A;&#x4E49;&#x597D;&#x7684;&#x5360;&#x4F4D;&#x53D8;&#x91CF;&#x540D;&#x3002;</li>
</ul>
<h3 id="&#x6E32;&#x67D3;&#x6A21;&#x7248;&#x51FD;&#x6570;">&#x6E32;&#x67D3;&#x6A21;&#x7248;&#x51FD;&#x6570;</h3>
<ul>
<li>Flask&#x63D0;&#x4F9B;&#x7684; <strong>render_template</strong> &#x51FD;&#x6570;&#x5C01;&#x88C5;&#x4E86;&#x8BE5;&#x6A21;&#x677F;&#x5F15;&#x64CE;</li>
<li><strong>render_template</strong> &#x51FD;&#x6570;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x662F;&#x6A21;&#x677F;&#x7684;&#x6587;&#x4EF6;&#x540D;&#xFF0C;&#x540E;&#x9762;&#x7684;&#x53C2;&#x6570;&#x90FD;&#x662F;&#x952E;&#x503C;&#x5BF9;&#xFF0C;&#x8868;&#x793A;&#x6A21;&#x677F;&#x4E2D;&#x53D8;&#x91CF;&#x5BF9;&#x5E94;&#x7684;&#x771F;&#x5B9E;&#x503C;&#x3002;</li>
</ul>
<h3 id="&#x4F7F;&#x7528;">&#x4F7F;&#x7528;</h3>
<h4 id="&#x6CE8;&#x91CA;">&#x6CE8;&#x91CA;</h4>
<ul>
<li>&#x4F7F;&#x7528; {# #} &#x8FDB;&#x884C;&#x6CE8;&#x91CA;</li>
</ul>
<pre><code>{# &#x8FD9;&#x662F;&#x6CE8;&#x91CA; #}
</code></pre><h4 id="&#x53D8;&#x91CF;&#x4EE3;&#x7801;&#x5757;">&#x53D8;&#x91CF;&#x4EE3;&#x7801;&#x5757;</h4>
<ul>
<li>{{}} &#x6765;&#x8868;&#x793A;&#x53D8;&#x91CF;&#x540D;&#xFF0C;&#x8FD9;&#x79CD; {{}} &#x8BED;&#x6CD5;&#x53EB;&#x505A;<strong>&#x53D8;&#x91CF;&#x4EE3;&#x7801;&#x5757;</strong></li>
</ul>
<pre><code class="lang-html">{{ post.title }}
</code></pre>
<p>Jinja2 &#x6A21;&#x7248;&#x4E2D;&#x7684;&#x53D8;&#x91CF;&#x4EE3;&#x7801;&#x5757;&#x53EF;&#x4EE5;&#x662F;&#x4EFB;&#x610F;Python&#x7C7B;&#x578B;&#x6216;&#x8005;&#x5BF9;&#x8C61;&#xFF0C;&#x53EA;&#x8981;&#x5B83;&#x80FD;&#x591F;&#x88AB;Python&#x7684;str()&#x65B9;&#x6CD5;&#x8F6C;&#x6362;&#x4E3A;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#x5C31;&#x53EF;&#x4EE5;,&#x6BD4;&#x5982;,&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x4E0B;&#x9762;&#x7684;&#x65B9;&#x5F0F;&#x663E;&#x793A;&#x4E00;&#x4E2A;&#x5B57;&#x5178;&#x6216;&#x8005;&#x5217;&#x8868;&#x4E2D;&#x7684;&#x67D0;&#x4E2A;&#x5143;&#x7D20;:</p>
<pre><code class="lang-python">{{your_dict[<span class="hljs-string">&apos;key&apos;</span>]}}
{{your_list[<span class="hljs-number">0</span>]}}
</code></pre>
<h4 id="&#x63A7;&#x5236;&#x4EE3;&#x7801;&#x5757;">&#x63A7;&#x5236;&#x4EE3;&#x7801;&#x5757;</h4>
<ul>
<li>&#x7528; {<strong>%%</strong>} &#x5B9A;&#x4E49;&#x7684;<strong>&#x63A7;&#x5236;&#x4EE3;&#x7801;&#x5757;</strong>&#xFF0C;&#x53EF;&#x4EE5;&#x5B9E;&#x73B0;&#x4E00;&#x4E9B;&#x8BED;&#x8A00;&#x5C42;&#x6B21;&#x7684;&#x529F;&#x80FD;&#xFF0C;&#x6BD4;&#x5982;&#x5FAA;&#x73AF;&#x6216;&#x8005;if&#x8BED;&#x53E5;</li>
</ul>
<pre><code class="lang-html">{% if user %}
    {{ user }}
{% else %}
    hello!

{% for index in indexs %}
    {{ index }} 
{% endfor %}
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../chapter2.html" class="navigation navigation-prev " aria-label="Previous page: 模板"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../chapter2/guo-lv-qi.html" class="navigation navigation-next " aria-label="Next page: 过滤器"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"toggle-chapters":{},"splitter":{},"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
    </body>
    <!-- End of book 6节课入门Flask框架web开发 -->
</html>
